package task

import (
	"cvevulner/common"
	"cvevulner/models"
	"cvevulner/taskhandler"
	"errors"
	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
	"strings"
)

//CheckOriCve Check the CVE of the original data
func CheckOriCve() error {
	defer common.Catchs()
	logs.Info("The task of verifying the original cve data starts...")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("CheckOriCve, config init, error:", err)
		return err
	}
	// The amount of data processed at a time
	prcNum, err := BConfig.Int("crontab::prcnum")
	if err != nil {
		logs.Error("CheckOriCve, config crontab::prcnum, error: invalid value")
		return errors.New("value is nil")
	}
	// Get the data source of the table
	_, err = taskhandler.CheckCveOriginData(prcNum)
	logs.Info("End of verifying original cve data task...")
	return err
}

// Unlock database table data
func UnLockTable() error {
	defer common.Catchs()
	logs.Info("Execute unlock table task start...")
	beforeTime := common.GetBeforeHourTime(1)
	// unlock center
	models.UnLockUpdateIssueStatus(15, 0, common.GetCurTime(), beforeTime)
	// unlock origin cve
	models.UnLockUpdateCveIssueStatus(15, 0, common.GetCurTime(), beforeTime)
	// unlock upstream
	models.UnLockUpdateOriginStatus(15, 0, common.GetCurTime(), beforeTime)
	// unlock excel
	models.UnLockUpdateOriginExcelStatus(15, 0, common.GetCurTime(), beforeTime)
	logs.Info("Execute unlock table End of task...")
	return nil
}

// Cve handles abnormal data reprocessing
func ProcAbnCve() error {
	defer common.Catchs()
	logs.Info("Correct the cve data and start the task...")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("ProcAbnCve, config init, error:", err)
		return err
	}
	// The amount of data processed at a time
	prcNum, err := BConfig.Int("crontab::prcnum")
	if err != nil {
		logs.Error("ProcAbnCve, config crontab::prcnum, error: invalid value")
		return errors.New("value is nil")
	}
	days, err := BConfig.Int("crontab::days")
	if err != nil {
		logs.Error("ProcAbnCve, config crontab::days, error: invalid value")
		return errors.New("value is nil")
	}
	cveStatuStr := BConfig.String("cve::abn_cve_status")
	if cveStatuStr == "" {
		logs.Error("ProcAbnCve, config cve::abn_cve_status error: invalid value")
		return errors.New("value is nil")
	}
	st := strings.Split(cveStatuStr, ",")
	for k, v := range st {
		st[k] = "'" + v + "'"
	}
	cveSt := strings.Join(st, ",")
	// Get the data source of the table
	_, err = taskhandler.UpdateAbnCveStatus(prcNum, days, cveSt)
	logs.Info("Correct cve data End of task...")
	return err
}

// Generate a certain number of SA numbers each time
func ProcSaNumber() error {
	defer common.Catchs()
	logs.Info("Generate SA number task start...")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("ProcSaNumber, config init, error:", err)
		return err
	}
	// The amount of data processed at a time
	saReAmount, err := BConfig.Int64("cve::sa_re_amount")
	if err != nil {
		logs.Error("ProcSaNumber, config cve::sa_re_amount, error: invalid value")
		return errors.New("value is nil")
	}
	// Get the data source of the table
	_, err = taskhandler.AddSaNumber(saReAmount)
	logs.Info("Generate SA number task end...")
	return err
}
